package com.liuzhilong.infrastructure.framework.spring.processor;

import com.liuzhilong.infrastructure.framework.spring.component.MyComponent;
import com.liuzhilong.infrastructure.framework.spring.component.ProxyMyComponet;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    /**
     * 测试 BeanFactoryPostProcessor当定义为{@link com.liuzhilong.infrastructure.framework.spring.component.MyComponent} 的时候，替换成为
     * {@link ProxyMyComponet}
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        for(String beanName:beanFactory.getBeanDefinitionNames()){
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
            if(MyComponent.class.getName().equalsIgnoreCase(beanDefinition.getBeanClassName())){
                beanDefinition.setBeanClassName(ProxyMyComponet.class.getName());
            }
        }
    }
}
